var tableId = 'table'+c.uuid();
var editArea = ['600px', '370px'];

$(function(){
    c.table({
        elem: '#table',
        url: '/user/page',
        id:tableId,
        height: 'full',
        cellMinWidth: 80,
        page: true,
        cols: [[
            {field:'name',title: '名称',width:100},
            {field:'status',title: '状态',width:90,templet:function(row){
                var sel = '';
                if( row.status == 0 ){
                    sel = '<span class="layui-badge layui-bg-cyan">已删除</span>';
                }else if( row.status == 1 ){
                    sel = '<span class="layui-badge layui-bg-blue">正常</span>';
                }else if( row.status == 2 ){
                    sel = '<span class="layui-badge">禁用</span>';
                }else if( row.status == 3 ){
                    sel = '<span title="请重置密码" class="layui-badge layui-bg-orange">不可登录</span>';
                }
                return sel;
            }},
            {field:'roles',title: '角色',width:200},
            {field:'phone',title: '电话',width:122},
            {field:'email',title: '邮箱',width:160},
            {field:'op',title: '操作',width:130,templet:function(row){
                var edit = '<a href="javascript:editRow(&apos;'+row.id+'&apos;);" class="bluea">编辑</a>';
                var del = '<a href="javascript:deleteRow(&apos;'+row.id+'&apos;);" class="reda">删除</a>';
                return edit+c.opSpace()+del;
            }}
        ]],
        error: function(res, msg){
            console.log(res, msg)
        }
    });

    $('#create').click(function(){
        c.window({
			area: editArea,
			content:'/html/basic/user_add.html'
		});
    });

    $('#search').click(function(){
        reloadTable({where:c.putPrefix(form.val('search'),'user.')});
    });
});

function reloadTable(queryParams){
    queryParams = queryParams ? queryParams : {};
    table.reloadData(tableId,queryParams,true);
}

function editRow(id){
    c.window({
        area: editArea,
        content:'/html/basic/user_add.html?id='+id
    });
}

function deleteRow(id){
    layer.confirm('确定要删除吗？', {icon: 3, title:'提示'}, function(index){
        c.request('user/delete',{id:id},function(resp){
            if( resp.code == 200 ){
                c.message('删除成功');
            }else{
                c.message(resp.message);
            }
            reloadTable();
        });
        layer.close(index);
    });
}
